<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="区域类型" v-bind="validateInfos.regionType">
	          <j-dict-select-tag v-model:value="formData.regionType" dictCode="regionType" placeholder="请选择区域类型" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="危险化学品名称" v-bind="validateInfos.name">
            <a-input v-model:value="formData.name" placeholder="请输入危险化学品名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="CAS号" v-bind="validateInfos.casNumber">
            <a-input v-model:value="formData.casNumber" placeholder="请输入CAS号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="化学品类型" v-bind="validateInfos.chemicalClass">
	          <j-dict-select-tag v-model:value="formData.chemicalClass" dictCode="chemicalClass" placeholder="请选择化学品类型" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="作用" v-bind="validateInfos.effect">
            <a-input v-model:value="formData.effect" placeholder="请输入作用" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="最大储存量（t）" v-bind="validateInfos.maxStorage">
	          <a-input-number v-model:value="formData.maxStorage" placeholder="请输入最大储存量（t）" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="实时储存量" v-bind="validateInfos.realTimeStorage">
	          <a-input-number v-model:value="formData.realTimeStorage" placeholder="请输入实时储存量" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="储存方式/地点" v-bind="validateInfos.storageMethodLocation">
            <a-input v-model:value="formData.storageMethodLocation" placeholder="请输入储存方式/地点" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="产品生产能力（吨）" v-bind="validateInfos.productiveCapacity">
	          <a-input-number v-model:value="formData.productiveCapacity" placeholder="请输入产品生产能力（吨）" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="产品生产能力气体（方）" v-bind="validateInfos.maxProductiveGas">
	          <a-input-number v-model:value="formData.maxProductiveGas" placeholder="请输入产品生产能力气体（方）" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="产品最大储量（吨）" v-bind="validateInfos.maxReservesTonsOf">
	          <a-input-number v-model:value="formData.maxReservesTonsOf" placeholder="请输入产品最大储量（吨）" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="产品最大储量气体（方）" v-bind="validateInfos.maxReservesCube">
	          <a-input-number v-model:value="formData.maxReservesCube" placeholder="请输入产品最大储量气体（方）" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="是否重点监管化学品" v-bind="validateInfos.isKeyChemicals">
            <j-dict-select-tag type='radio' v-model:value="formData.isKeyChemicals" dictCode="yn" placeholder="请选择是否重点监管化学品" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="是否易制爆" v-bind="validateInfos.isInhibitingExplosion">
            <j-dict-select-tag type='radio' v-model:value="formData.isInhibitingExplosion" dictCode="yn" placeholder="请选择是否易制爆" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="是否剧毒" v-bind="validateInfos.isHighlyToxic">
            <j-dict-select-tag type='radio' v-model:value="formData.isHighlyToxic" dictCode="yn" placeholder="请选择是否剧毒" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="是否易制毒" v-bind="validateInfos.isManufactureDrugs">
            <j-dict-select-tag type='radio' v-model:value="formData.isManufactureDrugs" dictCode="yn" placeholder="请选择是否易制毒" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="状态" v-bind="validateInfos.status">
            <j-dict-select-tag type='radio' v-model:value="formData.status" dictCode="normalOrStop" placeholder="请选择状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="上传文件" v-bind="validateInfos.doc">
	          <j-upload v-model:value="formData.doc"  :disabled="disabled" ></j-upload>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../TbBasicsChemicals.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    regionType: '',   
    name: '',   
    casNumber: '',   
    chemicalClass: '',   
    effect: '',   
    maxStorage: undefined,
    realTimeStorage: undefined,
    storageMethodLocation: '',   
    productiveCapacity: undefined,
    maxProductiveGas: undefined,
    maxReservesTonsOf: undefined,
    maxReservesCube: undefined,
    isKeyChemicals: '',   
    isInhibitingExplosion: '',   
    isHighlyToxic: '',   
    isManufactureDrugs: '',   
    status: '',   
    doc: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    regionType: [{ required: true, message: '请输入区域类型!'},],
    name: [{ required: true, message: '请输入危险化学品名称!'},],
    casNumber: [{ required: true, message: '请输入CAS号!'},],
    maxStorage: [{ required: true, message: '请输入最大储存量（t）!'},],
    realTimeStorage: [{ required: true, message: '请输入实时储存量!'},],
    storageMethodLocation: [{ required: true, message: '请输入储存方式/地点!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
